package com.cuz.hot100;

import com.cuz.daileetcode.utils.ArrayUtils;

public class Q59generateMatrix {

    public static void main(String[] args) {
        int[][] ints = generateMatrix1(4);
        ArrayUtils.print(ints);
    }

    public static int[][] generateMatrix1(int n) {
        if (n <= 0) {
            return new int[0][0];
        }
        int count = 1;
        int[][] res = new int[n][n];
        int top = 0;
        int dow = n - 1;
        int left = 0;
        int right = n - 1;
        while (count <= n * n) {
            for (int i = left; i <= right; i++) {
                res[top][i] = count++;
            }
            top++;
            for (int i = top; i <= dow; i++) {
                res[i][right] = count++;
            }
            right--;
            for (int i = right; i >= left; i--) {
                res[dow][i] = count++;
            }
            dow--;
            for (int i = dow; i >= top; i--) {
                res[i][left] = count++;
            }
            left++;
        }
        return res;
    }
}
